package eteam.aps.systemservice.dal;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import eteam.aps.systeminterface.model.entity.CustclassEntity;
import eteam.aps.systeminterface.model.entity.CustomerEntity;
import eteam.aps.systeminterface.model.enums.StateEnum;
import eteam.aps.systemservice.dal.mapper.ICustclassMapper;
import eteam.aps.systemservice.dal.mapper.ICustomerMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户资料数据操作类
 */
@Service
public class CustomerDal {
    @Autowired
    private ICustclassMapper custclassMapper;
    @Autowired
    private ICustomerMapper customerMapper;

    /**
     * 分页查询数据
     *
     * @param page:    分页参数
     * @param wrapper: 检索参数
     * @return
     */
    public IPage<CustomerEntity> selectPageList(IPage<CustomerEntity> page, Wrapper<CustomerEntity> wrapper) {
        return customerMapper.selectPage(page, wrapper);
    }

    /**
     * 根据分类ID获取下属客户数量
     *
     * @param classId: 分类ID
     * @return
     */
    public Integer getCustomerCountByPid(String classId) {
        QueryWrapper<CustomerEntity> wrapper = new QueryWrapper<CustomerEntity>();
        wrapper.ne("State", StateEnum.Delete.getValue());
        wrapper.eq("FClassId", classId);
        return customerMapper.selectCount(wrapper);
    }

    /**
     * 根据ID获取客户信息
     *
     * @param customerId
     * @return
     */
    public CustomerEntity getById(String customerId) {
        return customerMapper.selectById(customerId);
    }

    /**
     * 新增客户数据
     *
     * @return: 受影响行数
     */
    public int insertCustomer(CustomerEntity entity) {
        return customerMapper.insert(entity);
    }

    /**
     * 修改客户数据
     *
     * @return: 受影响行数
     */
    public int updateCustomer(CustomerEntity entity) {
        return customerMapper.updateById(entity);
    }

    /**
     * 获取客户编码
     *
     * @param prefix
     * @return
     */
    public String getCustCode(String prefix) {
        String maxNo = customerMapper.selectMaxCustCode(prefix);
        int seq = 0;
        if (StringUtils.isNotBlank(maxNo)) {
            seq = Convert.toInt(StrUtil.removePrefixIgnoreCase(maxNo, prefix));
        }
        return StrUtil.format("{}{}", prefix, NumberUtil.decimalFormat("0000", seq + 1));
    }


    /**
     * 根据分类ID获取实体对象
     *
     * @param classId: 分类ID
     * @return
     */
    public CustclassEntity getClassById(String classId) {
        return custclassMapper.selectById(classId);
    }

    /**
     * 根据父级分类ID获取下级分类列表
     *
     * @param currentOrgId: 当前登录机构Id
     * @param classId:      分类ID
     * @return
     */
    public List<CustclassEntity> getClassListByPid(String currentOrgId, String classId) {
        QueryWrapper<CustclassEntity> wrapper = new QueryWrapper<CustclassEntity>();
        wrapper.eq("State", StateEnum.Enable.getValue());
        if (StringUtils.isNotBlank(classId)) {
            wrapper.eq("FAboveId", classId);
            //wrapper.eq("FRecorderOrg", currentOrgId);
        } else {
            wrapper.and(i -> i.eq("FAboveId", "").or().isNull("FAboveId"));
        }
        wrapper.orderByAsc("FOrder");
        List<CustclassEntity> retList = custclassMapper.selectList(wrapper);
        for (CustclassEntity item : retList) {
            Integer count = custclassMapper.selectCount(new QueryWrapper<CustclassEntity>()
                    .eq("State", StateEnum.Enable.getValue())
                    .eq("FAboveId", item.getFclassid()));
            item.setIsleaf(count < 1);
        }
        return retList;
    }

    /**
     * 根据父级分类ID获取下级分类数量
     *
     * @param classId: 分类ID
     * @return
     */
    public Integer getClassCountByPid(String classId) {
        QueryWrapper<CustclassEntity> wrapper = new QueryWrapper<CustclassEntity>();
        wrapper.ne("State", StateEnum.Delete.getValue());
        wrapper.eq("FAboveId", classId);
        //if (StringUtils.isNotBlank(classId)) {
        //     wrapper.eq("FAboveId", classId);
        // } else {
        //     wrapper.and(i -> i.eq("FAboveId", "").or().isNull("FAboveId"));
        // }
        return custclassMapper.selectCount(wrapper);
    }

    /**
     * 新增分类数据
     *
     * @return: 受影响行数
     */
    public int insertClass(CustclassEntity entity) {
        return custclassMapper.insert(entity);
    }

    /**
     * 修改分类数据
     *
     * @return: 受影响行数
     */
    public int updateClass(CustclassEntity entity) {
        return custclassMapper.updateById(entity);
    }
}
